//
//  RMMMemberCardViewController.m
//  Romomo
//
//  Created by puman on 16/10/17.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMMemberCardViewController.h"
#import "RMMMemberCardView.h"
#import "RMMMemberCardSMViewController.h"
#import "RMMChongZhiViewController.h"
#import "RMMMemberCardListTableViewCell.h"
#import "RMMTiXianViewController.h"
#import "RMMAboutMineViewController.h"

@interface RMMMemberCardViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation RMMMemberCardViewController
{
    RMMMemberCardView *_cardView;
    RMMNotNetWorkView *_networkView;
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSString *_balance;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kCColor;
    self.navigationItem.title = @"会员卡";
    [self addViews];
    [self getData];
    // Do any additional setup after loading the view.
}
- (void)getData{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    NSString *cust_id = [RMMFileManager retrunObjWith:[RMMFileManager shareHandle].userDic[@"CUST_ID"]];
    parDic[@"cust_id"] = cust_id;
    parDic[@"org_no"] = Org_no;
    NSString *url = GetCustCardUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (isNetwork) {
            _networkView.hidden = YES;
        } else {
            _networkView.hidden = NO;
        }
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _dataArray = dataDic[@"data"][@"CARD_DE"];
            NSDictionary *dic = [RMMFileManager setValueForDic:dataDic[@"data"]];
            NSString *balance = [NSString stringWithFormat:@"%.2f",[dic[@"BALANCE"] floatValue]];
            _balance = [NSString stringWithFormat:@"%.2f",[dic[@"BALANCE"] floatValue]];
            if (_dataArray.count) {
                _cardView.labelT.text = @"使用记录";
            } else {
                _cardView.labelT.text = @"暂无记录";
            }
            _cardView.balanceLabel.text = [NSString stringWithFormat:@"¥%@",balance];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        [_tableView reloadData];
    } error:^(NSError *errors) {
        
    } view:self.view];
}
- (void)addViews{
    [self addRightItem];
    _dataArray = [NSMutableArray array];
    _cardView = [[RMMMemberCardView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 240)];
    BUTTON_S(_cardView.button, chongZhiBut);
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 0)];
    [_tableView registerClass:[RMMMemberCardListTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
    
    _networkView = [[RMMNotNetWorkView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    _networkView.hidden = YES;
    BUTTON_S(_networkView.button, networkBut);
    [self.view addSubview:_networkView];
}
- (void)networkBut{
    [self getData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return _cardView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 240;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RMMMemberCardListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dic = _dataArray[indexPath.row];
    return cell;
}
- (void)addAlertSheet{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *tiXianAction = [UIAlertAction actionWithTitle:@"申请提现" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        RMMTiXianViewController *VC = [[RMMTiXianViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }];
    UIAlertAction *shuoMingAction = [UIAlertAction actionWithTitle:@"会员卡说明" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        RMMAboutMineViewController *VC = [[RMMAboutMineViewController alloc] init];
        VC.titleName = @"会员卡说明";
        [self.navigationController pushViewController:VC animated:YES];
    }];
    UIAlertAction *callAction = [UIAlertAction actionWithTitle:@"联系客服" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [RMMFileManager callKeFuWithPhoneNumber:NumberSJ onView:self.view];
        
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    if (DEVICE >= 9.0) {
        [callAction setValue:kBasicColor forKey:@"titleTextColor"];
        [shuoMingAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
        [cancel setValue:COLORFROMCODE(0x9B9B9B, 1) forKey:@"titleTextColor"];
    }
    //[tiXianAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
   // [alertController addAction:tiXianAction];
    [alertController addAction:shuoMingAction];
    [alertController addAction:callAction];
    [alertController addAction:cancel];
    [self presentViewController:alertController animated:YES completion:nil];

}
- (void)addRightItem{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"展开图标@3x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightItemClick)];
    self.navigationItem.rightBarButtonItem = item;
    item.tintColor = RGBA(44, 44, 44, 1);
}
- (void)chongZhiBut{
    RMMChongZhiViewController *VC = [[RMMChongZhiViewController alloc] init];
    VC.balance = _balance;
    VC.memBlock = ^void(){
        [self getData];
    };
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)rightItemClick{
    [self addAlertSheet];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
